#pragma pack(4)
#include "myHeader.h"

#include "ttClient.h"
#include "ttServer.h"
#include "ttMsgMap.h"
#include "myUtil.h"
#include "btnManager.h"
#include "editManager.h"
#include "timerManager.h"
#include "winProcCallBacks.h"
#include "d3dDrawer.h"

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int InitWinProcCallBacks();
IttClient* Me = NULL;
bool bRunning = true;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
#ifdef CONSOLE_TEST
	StartConsole();
#endif
    myUtil::WSAInit();
	btnManager::Get()->setInstance(hInstance);
	editManager::Get()->setInstance(hInstance);
	timerManager::Get()->setInstance(hInstance);
	ttMsgMap::Get()->m_msglpvar.m_MainInstance = hInstance;
	InitWinProcCallBacks();

	static TCHAR szAppName[] = TEXT ("HelloWin") ;

	HWND   hwnd ;
	MSG    msg ;
	WNDCLASS wndclass ;

	wndclass.style        = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc  = WndProc ;
	wndclass.cbClsExtra   = 0 ;
	wndclass.cbWndExtra   = 0 ;
	wndclass.hInstance    = hInstance ;
	wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground= (HBRUSH) GetStockObject (GRAY_BRUSH) ;
	wndclass.lpszMenuName = NULL ;
	wndclass.lpszClassName= szAppName ;

	if (!RegisterClass (&wndclass))
	{
		MessageBox (  NULL, TEXT ("This program requires Windows NT!"),
				szAppName, MB_ICONERROR) ;
		return 0 ;
	}
	hwnd = CreateWindow( szAppName,      // window class name
			TEXT ("The Hello Program"),   // window caption
			WS_OVERLAPPEDWINDOW,  // window style
			CW_USEDEFAULT,// initial x position
			CW_USEDEFAULT,// initial y position
			CW_USEDEFAULT,// initial x size
			CW_USEDEFAULT,// initial y size
			NULL,                 // parent window handle
			NULL,            // window menu handle
			hInstance,   // program instance handle
			NULL) ;      // creation parameters
	d3dDrawer::Get()->init(hwnd);

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	static float lastTime = (float)timeGetTime();  
	memset(&msg, 0, sizeof(msg));
    while (msg.message != WM_QUIT)  
    {  
        if (PeekMessage(&msg,0,0,0,PM_REMOVE)){  
            TranslateMessage (&msg) ;  
            DispatchMessage (&msg) ;  
        }  
        else{  
            float currTime = (float)timeGetTime();  
            float timeDelta = (currTime - lastTime) * 0.001f;  
			d3dDrawer::Get()->render(timeDelta);  
            lastTime = currTime;  
        }  
    }

    myUtil::WSAUnint();
	return msg.wParam ;
}

int InitWinProcCallBacks()
{
	ttMsgMap::Get()->setDefFunc(new onDefault);
#define PUSHMSG(name) ttMsgMap::Get()->push_front(new name);
	PUSHMSG(onCreate)
	PUSHMSG(onDestroy)
	PUSHMSG(onBtnClick)
	PUSHMSG(onSize)
	PUSHMSG(onTimer)
	return 0;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	return ttMsgMap::Get()->run(hwnd, message, wParam, lParam);
}